<?php
include 'includes/application_top.inc';
include 'includes/paginate.php';
include 'auth.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Easy Living - Responsive Real Estate Template">
<meta name="keywords" content="Themes, real estate, responsive, themeforest, Templates">
<meta name="author" content="Rype Pixel [Chris Gipple]">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome Listing grid Reports</title>
<!-- html5 support in IE8 and later -->
<script src="../../html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<!-- CSS file links -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/styles.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/responsive.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<!-- Start Header -->
<?php include 'includes/header.php';?>
<!-- End Header -->
<!-- start subheader -->
<p>&nbsp;</p>
<!-- start recent properties -->
<section class="properties">
    <div class="container">
    	<?php include 'includes/left.php';?>
    
        <div class="row">
            <div class="col-sm-6">
                <div class="propertyItem">
                	<!--  onsubmit="validate();"-->
                	
                		<?php   
								 $datepickerstart = @$_REQUEST['datepickerstart'];
								 $datepickerend = @$_REQUEST['datepickerend'];
								 $search = @$_REQUEST['search'];
                		
						 $curRequest = "&datepickerstart=".$datepickerstart."&datepickerend=".$datepickerend."&search=".$search;
								 
								                 		
                		        $user_id = $_SESSION['u_id'];
								$RefAuthid = 5;
								$authCheck  = authentication($RefAuthid,$user_id); 
								if($authCheck==1)
								{?>
					
                	<?php 	$search1 = trim(@$_REQUEST['search'])."&nbsp;".trim(@$_REQUEST['datepickerstart'])."&nbsp;".trim(@$_REQUEST['datepickerend']);?>
                	 <form name="frm1" id="frm1" action="listing_grid_report.php" method="GET">
                	<table border="1" class="propertyDetails">
                		<tr >
                				<td>Start Date:</td><td colspan="2"><input type="text" id="datepickerstart" name="datepickerstart" value="<?php echo @$_REQUEST['datepickerstart'];?>"> &nbsp;&nbsp;End Date:  <input type="text" id="datepickerend" name="datepickerend" value="<?php echo @$_REQUEST['datepickerend'];?>"></td>
                			</tr>
                		<tr><td>Search by Url</td><td><input type="text" name="search" id="search" value="<?php echo @$_REQUEST['search'];?>"> </td><td><input type="submit" name="submit" value="Submit" /><a href="listing_grid_report.php">Clear All</a></td></tr>
                    <?php
                    if($search!=''){
                    echo '<tr><td colspan="5">Search by: '.$search1.'</td></tr>'; } ?></table> 
                	 </form>
                	 
					<table border="1" class="propertyDetails">
                         <tr class="tabHead">
                       	    <td>MSISDN</td>
                            <td>User Agent</td>
                            <td>OS</td>
                            <td>Date </td>
                            <td>Incoming Url</td>
                            <td>Outgoing Url</td>
                         </tr>
                    	<?php   
                    	$per_page = 25;  
						$datepickerstart = date("Y-m-d H:i:s",strtotime(@$_REQUEST['datepickerstart']));
						$datepickerend = date("Y-m-d H:i:s",strtotime(@$_REQUEST['datepickerend']));
						
						    $select = "SELECT * FROM track_url  ";
						 
						   	if(@trim($_REQUEST['datepickerstart'])!="" || @trim($_REQUEST['datepickerstart'])!="" || trim(@$_REQUEST['search'])!="")
							  {
							  	$select .=" WHERE ";
							  }
						       if(trim(@$_REQUEST['search'])!="")
								{
									$search = trim(@$_REQUEST['search']);
								  $select .= " `shorturl_code` LIKE '%$search%' OR `long_url` LIKE '%$search%' ";
								}
							  if(@trim($_REQUEST['datepickerstart'])!="" && @trim($_REQUEST['datepickerstart'])!="" && trim(@$_REQUEST['search'])!="")
								{
								 $select .= " AND ";	
								}
							  if(@$_REQUEST['datepickerstart']!="" && @$_REQUEST['datepickerstart']!="")
								{
							      $select .=" (datetime BETWEEN '$datepickerstart' AND '$datepickerend') ";	
								}
							 if(@trim($_REQUEST['datepickerstart'])=="" && @trim($_REQUEST['datepickerstart'])=="" && trim(@$_REQUEST['search'])=="")
							  {
							  	$select .=" WHERE shorturl_code` LIKE '%testprtocess%'";
							  }
						 
						 $select .= " ORDER BY  t_id DESC";
					 // echo $select;
					 	
						$result = mysql_query($select);
						$total_results = @mysql_num_rows($result);
						$pagesUrl = "tpages=".@$_REQUEST['tpages']."&page=".@$_REQUEST['page'];
						$total_pages = ceil($total_results / $per_page);//total pages we going to have
						//-------------if page is setcheck------------------//
						if (isset($_GET['page'])) {
						    $show_page = $_GET['page'];             //it will telles the current page
						    if ($show_page > 0 && $show_page <= $total_pages) {
						        $start = ($show_page - 1) * $per_page;
						        $end = $start + $per_page;
						    } else {
						        // error - show first set of results
						        $start = 0;              
						        $end = $per_page;
						    }
						} else {
						    // if page isn't set, show first set of results
						    $start = 0;
						    $end = $per_page;
						}
						// display pagination
						$page = intval(@$_GET['page']);
						
						$tpages=$total_pages;
						if ($page <= 0)
						    $page = 1;
                    	
						 $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
						 
                    echo '<div class="pagination"><ul>';
                    if ($total_pages > 1) {
                       // echo paginate($reload, @$show_page, $total_pages);
						 
					echo 	paginationprocess($reload, @$show_page, $total_pages,$curRequest);
					 
                    }
                    echo "</ul></div>";
					
					if($total_results>0)
					{
					
						for ($i = $start; $i < $end; $i++) {
	                        // make sure that PHP doesn't try to show results that don't exist
	                        if ($i == $total_results) {
	                            break;
	                        }
							
	                       $id= mysql_result($result, $i, 't_id');
						   $long_url  =mysql_result($result, $i, 'long_url');
						   $useragent  =mysql_result($result, $i, 'useragent');
						   $shorturl_code = ROOTURL. mysql_result($result, $i, 'shorturl_code');
						   $long_url  =mysql_result($result, $i, 'long_url');
						   $msisdn  =mysql_result($result, $i, 'msisdn');
						   $os  =mysql_result($result, $i, 'os');
	                        // echo out the contents of each row into a table
	                        echo '<tr class="propertyDetails1">';
	                        echo '<td>' .$msisdn . '</td>';
							echo '<td>'.$useragent.'</td>';
							echo '<td>' .$os . '</td>';
							 echo '<td>' . mysql_result($result, $i, 'datetime') . '</td>';
	                        echo '<td><a target="_blank" href="'.$shorturl_code .'">' .$shorturl_code . '</a></td>';
	                         echo '<td>' . $long_url. '</td>';                    
	                        echo "</tr>";
	                    }  
	                 }else
					 {
					 echo "<tr><td colspan='6'>Record Not Found</td></tr>";	
					 }
					
					?>                       
                    </table>
                    <?php
				}else { echo '<p align="center"></br></br></br><b>You have not permission to access.</b></br></br></br></br></br></br></br></br></br></br></br></br></p>';}	?>
				<hr />
                </div>
            </div>
        </div><!-- end row -->
    </div><!-- end container -->
</section>
<!-- end recent properties -->
<div class="bottomBar">
    <div class="container">
        <p>&nbsp;</p>
    </div>
</div>

<!-- JavaScript file links -->
<script src="js/jquery.js"></script>			<!-- Jquery -->
<script src="js/bootstrap.min.js"></script>  <!-- bootstrap 3.0 -->
<script src="js/respond.js"></script>


 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<!-- Jquery datepicker End-->
 <script>
$(function() {
$( "#datepickerstart" ).datepicker();
});
$(function() {
$( "#datepickerend" ).datepicker();
});
</script>

<script type="text/javascript">
    function validate() {
        var url = document.getElementById("url").value;
        var pattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        if (pattern.test(url)) {
            alert("Url is valid");
            return true;
        }else
        {
        	alert("Url is not valid!");
            return false;

        } 
            
    }
</script>
</body>
</html>